package com.cskaoyan.service.wx;

import com.cskaoyan.bean.mallbean.Region;
import com.cskaoyan.bean.mallbean.RegionExample;
import com.cskaoyan.bean.userbean.Address;
import com.cskaoyan.bean.userbean.AddressExample;
import com.cskaoyan.mapper.mallmapper.RegionMapper;
import com.cskaoyan.mapper.usermapper.AddressMapper;
import com.cskaoyan.vo.wx.WxAddressDetailVo;
import com.cskaoyan.vo.wx.WxAddressListVo;
import javafx.scene.input.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxAddressServiceImpl implements WxAddressService {
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;

    /**
     * 根据userId查询用户地址信息
     *
     * @param userId
     * @return vo
     */
    @Override
    public List<WxAddressListVo> list(int userId) {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        //设置example userId
        criteria.andUserIdEqualTo(userId);
        //设置delete = 0
        criteria.andDeletedEqualTo(false);


        //获得数据集
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        //塞到vo
        //根据省市县id拿到对应的str
        ArrayList<WxAddressListVo> wxAddressListVos = new ArrayList<>();
        for (Address address : addresses) {
            List<Region> regionList = getDetailToArea(address);
            String detailToArea = "";
            for (Region region : regionList) {
                detailToArea += region.getName();
            }
            WxAddressListVo wxAddressListVo = new WxAddressListVo();
            wxAddressListVo.setIsDefault(address.getIsDefault());
            wxAddressListVo.setDetailedAddress(detailToArea + " " + address.getAddress());
            wxAddressListVo.setName(address.getName());
            wxAddressListVo.setMobile(address.getMobile());
            wxAddressListVo.setId(address.getId());
            wxAddressListVos.add(wxAddressListVo);
        }

        return wxAddressListVos;
    }

    /**
     * 根据省市县id拿到详细到县的地址用于详细地址的拼接
     * TODO 后续可以优化一下
     *
     *
     * @return
     */
    private List<Region> getDetailToArea(Address address) {
        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(address.getProvinceId());
        integers.add(address.getCityId());
        integers.add(address.getAreaId());
        criteria.andIdIn(integers);
        return  regionMapper.selectByExample(regionExample);

    }

    /**
     * 根据id获取地址详情
     *
     * @param id
     * @return
     */
    @Override
    public WxAddressDetailVo detail(Integer id) {
        //得到地址详情
        Address address = addressMapper.selectByPrimaryKey(id);
        //得到省市县名称
        List<Region> detailToArea = getDetailToArea(address);
        //数据塞入vo
        return new WxAddressDetailVo().setProvinceName(detailToArea.get(0).getName()).setCityName(detailToArea.get(1).getName()).setAreaName(detailToArea.get(2).getName())
                .setProvinceId(address.getProvinceId()).setCityId(address.getCityId()).setAreaId(address.getAreaId()).setAddress(address.getAddress())
                .setName(address.getName()).setMobile(address.getMobile()).setDefault(address.getIsDefault()).setId(address.getId());

    }

    /**
     * 在region表中根据pid查询地址
     *
     * @param pid
     * @return
     */
    @Override
    public List<Region> listOfRegion(Integer pid) {
        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        criteria.andPidEqualTo(pid);
        //获取数据集
        return regionMapper.selectByExample(regionExample);

    }

    /**
     * 新增或修改用户地址信息
     *参考淘宝，可以没有默认地址，若有，则只能有一个
     * @param address
     * @return
     */
    @Override
    public Integer save(Address address) {
        //时间
        Date now = new Date(System.currentTimeMillis());
        address.setUpdateTime(now);

        //检查是否设置为默认地址
        //若新地址为默认地址，则将以前的默认地址设为非默认
        if (address.getIsDefault() == true){
            //设置查询条件
            AddressExample addressExample = new AddressExample();
            AddressExample.Criteria criteria = addressExample.createCriteria();
            criteria.andUserIdEqualTo(address.getUserId());
            criteria.andIsDefaultEqualTo(true);

            Address address1 = new Address();
            address1.setUpdateTime(now);
            address1.setIsDefault(false);
            addressMapper.updateByExampleSelective(address1,addressExample);

        }

        //检查是增还是改
        //增
        if (address.getId() == 0) {
            address.setAddTime(now);
            addressMapper.insert(address);
        }
        //改
        else {
            addressMapper.updateByPrimaryKeySelective(address);
        }

        return address.getId();
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        Address address = new Address();
        address.setId(id);
        address.setDeleted(true);
        addressMapper.updateByPrimaryKeySelective(address);
    }
}
